package edu.scut.dap.client;


import edu.scut.dap.util.StartRequest;
import edu.scut.dap.service.TaskCMService;
import edu.scut.dap.util.StateRequest;
import edu.scut.dap.util.StateResponse;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Created by Alex on 2016/12/3.
 */
public class TaskcmClient {

    private static Logger logger = Logger.getLogger(TaskcmClient.class);

    public static void main(String[] args) {
        String ip = "192.168.10.250";
        int port = 9876;
        TSocket transport = new TSocket(ip, port, 30000);
        logger.info("Startup client at ip: " + ip + " and in port: " + port);

        TProtocol protocol = new TBinaryProtocol(transport);
        final TaskCMService.Client client = new TaskCMService.Client(protocol);

        try {
            transport.open();
        } catch (TTransportException e) {
            e.printStackTrace();
            logger.error("Fail to connect with server");
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        Thread.sleep(30*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    try {
                        List<StateResponse> responseList=client.allTaskState();
                        System.out.println(new Date().toLocaleString());
                        for (StateResponse response:responseList) {
                            System.out.println(response);
                        }
                    } catch (TException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        for (int i = 0; i < 1; i++) {
            List<String> taskArgs = Arrays.asList(args);
            taskArgs = taskArgs.subList(3, 4);
            System.out.println(taskArgs);
            StartRequest startRequest = new StartRequest()
                    .setOperatorName(args[0])
                    .setInput(args[1])
                    .setOutput(args[2]+new Date().getTime())
                    .setTaskArgs(taskArgs)
                    .setTaskName("test task");
            System.out.println(startRequest.toString());
            try {
                int taskid = client.startTask(startRequest);
                try {
                    Thread.sleep(new Random().nextInt(30)*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                StateResponse response=client.taskState(new StateRequest(taskid));
                System.out.println(response);
            } catch (TException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
//            String output=client.getOutputMessage(new StateRequest(0));
//            System.out.println(output);
            String error=client.getErrorMessage(new StateRequest(0));
            System.out.println(error);
        } catch (TException e) {
            e.printStackTrace();
        }
    }
}
